﻿using System;
using System.Collections.Generic;
using System.Data.Linq.Mapping;
using Microsoft.Practices.Unity;
using SpectrumAnalyser.Common.InstanceContext;
using SpectrumAnalyser.Data.Common.Contracts;
using SpectrumAnalyser.Data.DataBase.Contracts;
using SpectrumAnalyser.Data.DataBase.MSSQL.Mapping;
using SpectrumAnalyser.Data.DataBase.Repositories;

namespace SpectrumAnalyser.Data.DataBase
{
    public class DataModule
    {
        private readonly IUnityContainer _unityContainer;

        public DataModule(IUnityContainer unityContainer)
        {
            _unityContainer = unityContainer;
        }

        public void Initialize()
        {
            _unityContainer.RegisterType<IRepositoryFactory, RepositoryFactory>();

            foreach (var repositoryType in RepositoryTypes)
            {
                _unityContainer.RegisterType(repositoryType.Key, repositoryType.Value);
            }

            /*foreach (var type in Mapping())
            {
                var instance = (IDbEntitiesMapping) Activator.CreateInstance(type.Value);
                _unityContainer.RegisterInstance(typeof (IDbEntitiesMapping), instance.InstanceType, instance);
                _unityContainer.RegisterInstance(typeof (MappingSource), instance.InstanceType,
                                                 instance.GetMappingSource());
            }*/
        }

        protected virtual IDictionary<Type, Type> RepositoryTypes
        {
            get
            {
                return new Dictionary<Type, Type>
                           {
                               {typeof (ITestRepositoryContract), typeof (TestRepository)}
                           };
            }
        }

        protected virtual IDictionary<Type, Type> Mapping()
        {
            return new Dictionary<Type, Type>
                {
                    {typeof (IDbEntitiesMapping), typeof (MsSqlDbMapping)}
                };
        }
    }
}